﻿// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.Web.Host</Project>
// <FileName>RequestInfo.cs</FileName>
// <CreateTime>2017-05-03 13:39</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-05-03 13:39" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System;
using Abp.Dependency;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Primitives;

namespace Vecen.BangChuang.Web.Host.Startup {

    /// <summary>
    /// 注入请求信息
    /// </summary>
    public class RequestInfo: IRequestInfo, ITransientDependency {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public RequestInfo(IHttpContextAccessor httpContextAccessor) {
            StringValues userAgent;
            if(httpContextAccessor.HttpContext.Request.Headers.TryGetValue("User-Agent", out userAgent)) {
                UserAgent = userAgent.ToString();
            }
            RemoteAddress = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
           
            RequestUri = new Uri(httpContextAccessor.HttpContext.Request.GetDisplayUrl());

            HostBaseUri = RequestUri.Scheme + "://" + RequestUri.Host;
        }

        /// <summary>
        /// 浏览器Agent
        /// </summary>
        public string UserAgent { get; }

        /// <summary>
        /// 请求IP
        /// </summary>
        public string RemoteAddress { get; }

        /// <summary>
        /// 请求地址
        /// </summary>
        public Uri RequestUri { get; }

        /// <summary>
        /// 当前应用网址
        /// </summary>
        public string HostBaseUri { get; }
    }
}